//
//  SHComplaintsView.m
//  youjian03
//
//  Created by Dadeso on 14/11/9.
//  Copyright (c) 2014年 com.shouhuobao. All rights reserved.
//

#import "SHComplaintsView.h"

#define TEXTVIEW_STATE  [self.wordTextView.text isEqualToString:@"用的不爽，说两句..."]

@interface SHComplaintsView (){
    int _complaintViewSign;
}
//匿名按钮状态
@property (nonatomic,copy) NSString * anonymousType;
@end

@implementation SHComplaintsView

-(id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [SHUtilitiesTool colorWithHexString:@"#f7f7f7"];
        HMLog(@"%f********%f",HMScreenW,HMScreenH);
        
        //保存初始化坐标
        _selfViewRect = SHRectMake(20, 84, HMScreenW -40, HMScreenH - 168);
        
        UILabel * label = [[UILabel alloc] initWithFrame:SHRectMake(0, 30, 280, 20)];
        label.text = @"打小报告";
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:19.0f];
        label.textColor = [SHUtilitiesTool colorWithHexString:@"#666666"];
        [self addSubview:label];
        
        /*
         
         ******  下面按钮为多选按钮SHConditionSelectedButton
         
         */
        SHConditionSelectedButton * btnOne = [[SHConditionSelectedButton alloc] initWithButtonTag:@"tagOne"];
        btnOne.frame = SHRectMake(30, 70, 110, 24);
        btnOne.delegate = self;
        [btnOne setTitle:@"拒收包裹" forState:UIControlStateNormal];
        [btnOne setTitleColor:[SHUtilitiesTool colorWithHexString:@"#999999"] forState:UIControlStateNormal];
        [btnOne setTitleColor:[SHUtilitiesTool colorWithHexString:@"#666666"] forState:UIControlStateSelected];
        [btnOne.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
        [btnOne setHLight:NO];
        [self addSubview:btnOne];
        
        SHConditionSelectedButton * btnTwo = [[SHConditionSelectedButton alloc] initWithButtonTag:@"tagTwo"];
        btnTwo.frame = SHRectMake(160, 70, 110, 24);
        btnTwo.delegate = self;
        [btnTwo setTitle:@"临时抬价" forState:UIControlStateNormal];
        [btnTwo setTitleColor:[SHUtilitiesTool colorWithHexString:@"#999999"] forState:UIControlStateNormal];
        [btnTwo setTitleColor:[SHUtilitiesTool colorWithHexString:@"#666666"] forState:UIControlStateSelected];
        [btnTwo.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
        [btnTwo setHLight:NO];
        [self addSubview:btnTwo];
        
        SHConditionSelectedButton * btnThree = [[SHConditionSelectedButton alloc] initWithButtonTag:@"tagThree"];
        btnThree.frame = SHRectMake(30, 107, 110, 24);
        btnThree.delegate = self;
        [btnThree setTitle:@"延迟取件" forState:UIControlStateNormal];
        [btnThree setTitleColor:[SHUtilitiesTool colorWithHexString:@"#999999"] forState:UIControlStateNormal];
        [btnThree setTitleColor:[SHUtilitiesTool colorWithHexString:@"#666666"] forState:UIControlStateSelected];
        [btnThree.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
        [btnThree setHLight:NO];
        [self addSubview:btnThree];
        
        SHConditionSelectedButton * btnFour = [[SHConditionSelectedButton alloc] initWithButtonTag:@"tagFour"];
        btnFour.frame = SHRectMake(160, 107, 110, 24);
        btnFour.delegate = self;
        [btnFour setTitle:@"未穿工服" forState:UIControlStateNormal];
        [btnFour setTitleColor:[SHUtilitiesTool colorWithHexString:@"#999999"] forState:UIControlStateNormal];
        [btnFour setTitleColor:[SHUtilitiesTool colorWithHexString:@"#666666"] forState:UIControlStateSelected];
        [btnFour.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
        [btnFour setHLight:NO];
        [self addSubview:btnFour];
        
        UIView * view = [[UIView alloc] initWithFrame:SHRectMake(15, 140, 250, 0.5)];
        view.backgroundColor = [SHUtilitiesTool colorWithHexString:@"#dddddd"];
        [self addSubview:view];
        
        UITextView * textView = [[UITextView alloc] initWithFrame:SHRectMake(15, 155, 250, 125)];
        textView.text = @"用的不爽，说两句...";
        textView.delegate = self;
        textView.textColor = [SHUtilitiesTool colorWithHexString:@"#cccccc"];
        [textView setFont:[UIFont systemFontOfSize:15.0f]];
        [self addSubview:textView];
        self.wordTextView = textView;
        
        SHConditionSelectedButton * btnFive = [[SHConditionSelectedButton alloc] initWithButtonTag:@"tagFive"];
        btnFive.frame = SHRectMake(17, 295, 110, 24);
        btnFive.delegate = self;
        btnFive.tag = ButtonComplaintAnonymous;
        [btnFive setTitle:@"匿名评价" forState:UIControlStateNormal];
        self.anonymousType = @"0";
        [btnFive setTitleColor:[SHUtilitiesTool colorWithHexString:@"#999999"] forState:UIControlStateNormal];
        [btnFive setTitleColor:[SHUtilitiesTool colorWithHexString:@"#666666"] forState:UIControlStateSelected];
        [btnFive.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
        [btnFive setHLight:NO];
        [self addSubview:btnFive];
        
        float widthW = [self ToCalculateTheCellWidth:[UIFont systemFontOfSize:15.0f] andLabelText:@"0/140"];
        UILabel * wordCountLabels = [[UILabel alloc] initWithFrame:SHRectMake(250 - widthW, 297, widthW, 16)];
        wordCountLabels.font = [UIFont systemFontOfSize:15];
        wordCountLabels.textColor = [SHUtilitiesTool colorWithHexString:@"#cccccc"];
        wordCountLabels.textAlignment = NSTextAlignmentRight;
        wordCountLabels.text = @"0/140";
        [self addSubview:wordCountLabels];
        self.wordCountLabel = wordCountLabels;
        
        UIView * view1 = [[UIView alloc] initWithFrame:SHRectMake(0, HMScreenH - 168 - 44, 280, 0.5)];
        view1.backgroundColor = [SHUtilitiesTool colorWithHexString:@"#cccccc"];
        [self addSubview:view1];
        
        UIView * view2 = [[UIView alloc] initWithFrame:SHRectMake(140.5, HMScreenH - 168 - 44.5, 0.5, 44)];
        view2.backgroundColor = [SHUtilitiesTool colorWithHexString:@"#cccccc"];
        [self addSubview:view2];
        
        //发表取消按钮
        UIButton * btn5 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn5.frame = SHRectMake(0, view2.frame.origin.y, 140, 44);
        btn5.tag = ButtonComplaintSure;
        [btn5 setTitle:@"发表" forState:UIControlStateNormal];
        [btn5 setTitleColor:[SHUtilitiesTool colorWithHexString:@"#cccccc"] forState:UIControlStateNormal];
        btn5.titleLabel.font = [UIFont systemFontOfSize:17.0f];
        //[btn5 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn5 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn5];
        self.btnPublish = btn5;
        
        UIButton * btn6 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn6.frame = SHRectMake(140.5, view2.frame.origin.y, 139.5, 44);
        btn6.tag = ButtonComplaintCancel;
        [btn6 setTitle:@"取消" forState:UIControlStateNormal];
        btn6.titleLabel.font = [UIFont systemFontOfSize:17.0f];
        [btn6 setTitleColor:[SHUtilitiesTool colorWithHexString:@"#666666"] forState:UIControlStateNormal];
        [btn6 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn6];
        
        UIControl * control = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, HMScreenW, HMScreenH)];
        [control addTarget:self action:@selector(controlClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:control];
        [self sendSubviewToBack:control];
        
        _complaintStr = [[NSMutableString alloc] init];
        //[_complaintStr appendString:@""];
        //注册键盘通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

- (void)keyboardWillHide:(NSNotification *)n
{
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    // The kKeyboardAnimationDuration I am using is 0.3
    [UIView setAnimationDuration:0.5];
    [self setFrame:_selfViewRect];
    [UIView commitAnimations];
}

-(void)keyboardWillShow:(NSNotification *)n{
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    // The kKeyboardAnimationDuration I am using is 0.3
    [UIView setAnimationDuration:0.5];
    [self setFrame:SHRectMake(_selfViewRect.origin.x, _selfViewRect.origin.y - 80, _selfViewRect.size.width, _selfViewRect.size.height)];
    [UIView commitAnimations];
}

//点击空白收起键盘
-(void)controlClick{
    [self.wordTextView resignFirstResponder];
    
}
//代理匿名评价状态
-(void)SHAnonymousState:(BOOL)anonymousState{
    if (anonymousState == true) {
        self.anonymousType = @"1";
    }else{
        self.anonymousType = @"0";
    }
    //self.anonymousType = anonymousState;
}
//发表，取消按钮点击事件
-(void)btnClick:(UIButton *)btn{
    if (btn.tag == ButtonComplaintSure) {
        if (_complaintStr.length > 0  || (self.wordTextView.text.length > 0 && TEXTVIEW_STATE == 0)) {
            NSDictionary *dic = @{@"anonymous":self.anonymousType,@"content":self.wordTextView.text,@"deliverId":self.deliverId ,@"token":self.tokenStr,@"complaint":_complaintStr};
            [SHHTTPTOOL PostOrderWithDic:dic andAction:@"userComplaint" success:^(id responseObj) {
                [_delegate disPlayToastView];
                [self removeFromSuperview];
                [_delegate removeComplaintView];
            } failure:^(NSError *error) {
                
            }];
        }
    }else if (btn.tag == ButtonComplaintCancel){
        [self removeFromSuperview];
        [_delegate removeComplaintView];
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}


-(void)textViewDidBeginEditing:(UITextView *)textView{
    textView.text = @"";
    
    
}
/*
 
 ***** 在textView中输入文字 右下角字数随之改变
 
 */
-(void)textViewDidChange:(UITextView *)textView{
    
    HMLog(@"%d",textView.text.length);
    if (textView.text.length > 140) {
        self.wordTextView.editable = NO;
        [_delegate textViewCountBeyond];
    }else{
        self.wordTextView.editable = YES;
        self.wordCountLabel.text = [NSString stringWithFormat:@"%d/140",textView.text.length];
        float widthW = [self ToCalculateTheCellWidth:[UIFont systemFontOfSize:15.0f] andLabelText:self.wordCountLabel.text];
        [self.wordCountLabel setFrame:SHRectMake(250 - widthW, 297, widthW, 16)];
    }
}

-(void)textViewDidEndEditing:(UITextView *)textView{
    if (textView.text.length > 0 || _complaintStr.length > 0) {
        UIButton * btn = (UIButton *)[self viewWithTag:ButtonComplaintSure];
        [btn setTitleColor:[SHUtilitiesTool colorWithHexString:@"#666666"] forState:UIControlStateNormal];
    }else if (textView.text.length == 0 && _complaintStr.length == 0){
        UIButton * btn = (UIButton *)[self viewWithTag:ButtonComplaintSure];
        [btn setTitleColor:[SHUtilitiesTool colorWithHexString:@"#cccccc"] forState:UIControlStateNormal];
    }
    if (textView.text.length == 0) {
        self.wordTextView.text = @"用的不爽，说两句...";
    }
}
/*

*****   代理数组，判断文字及发表取消按钮的字体颜色

*/
-(void)SHButtonDelegate:(NSMutableArray *)buttonSelectedTitleArray{
    NSMutableString * comPlaintString = [[NSMutableString alloc] init];
    for (NSDictionary * dict in buttonSelectedTitleArray) {
        for (NSString * str in @[@"tagOne",@"tagTwo",@"tagThree",@"tagFour",@"tagFive"]) {
            NSString * strs = [dict objectForKey:str];
            if (strs.length > 0 && ![strs isEqualToString:@"匿名评价"]) {
                [comPlaintString appendString:[dict objectForKey:str]];
                [comPlaintString appendString:@","];
            }
            HMLog(@"dict is %@************%d",[dict objectForKey:str],buttonSelectedTitleArray.count);
            HMLog(@"complaintStr is %@",_complaintStr);
        }
    }
    _complaintStr = comPlaintString;
    if (_complaintStr.length > 0  || (self.wordTextView.text.length > 0 && TEXTVIEW_STATE == 0)) {
        UIButton * btn = (UIButton *)[self viewWithTag:ButtonComplaintSure];
        [btn setTitleColor:[SHUtilitiesTool colorWithHexString:@"#666666"] forState:UIControlStateNormal];
    }else if ((self.wordTextView.text.length == 0 || TEXTVIEW_STATE == 1)&& (_complaintStr.length == 0)){
        UIButton * btn = (UIButton *)[self viewWithTag:ButtonComplaintSure];
        [btn setTitleColor:[SHUtilitiesTool colorWithHexString:@"#cccccc"] forState:UIControlStateNormal];
    }
}

-(CGFloat)ToCalculateTheCellWidth:(UIFont *)labelFont andLabelText:(NSString *)labelText{
    NSDictionary *attrs = @{NSFontAttributeName : labelFont};
    CGFloat titleW = [labelText boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.width;
    return titleW;
}


@end
